package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.entity.MovieInstance;
import la.iok.hzsvn.lewin.movie.model.PlayRoleBookVo;
import la.iok.hzsvn.lewin.movie.service.MovieDownloadService;
import la.iok.hzsvn.lewin.movie.service.MovieInstanceService;
import la.iok.hzsvn.lewin.movie.service.PlayRoleBookInstanceService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("playRoleBookInstances")
public class PlayRoleBookInstanceController {
    private final MovieDownloadService movieDownloadService;
    private final PlayRoleBookInstanceService playRoleBookInstanceService;
    private final MovieInstanceService movieInstanceService;

    public PlayRoleBookInstanceController(MovieDownloadService movieDownloadService,
                                          PlayRoleBookInstanceService playRoleBookInstanceService,
                                          MovieInstanceService movieInstanceService) {
        this.movieDownloadService = movieDownloadService;
        this.playRoleBookInstanceService = playRoleBookInstanceService;
        this.movieInstanceService = movieInstanceService;
    }

    /**
     * 查询剧本的所有电子书
     * @param token 操作用户
     * @param id 剧本id
     * @return 电子书列表
     */
    @GetMapping("movie/{id}")
    public ListDataResponse<PlayRoleBookVo> books(@RequestHeader Token token, @PathVariable Long id){
        Long inst = movieDownloadService.loadDownloadInstance(token,id);
        return new ListDataResponse<>(playRoleBookInstanceService.books(inst));
    }

    /**
     * 查询剧本的所有电子书
     * @param id 剧本id
     * @return 电子书列表
     */
    @GetMapping("movie/{id}/v/{version}")
    public ListDataResponse<PlayRoleBookVo> books2(@PathVariable Long id, @PathVariable String version){
        MovieInstance mi = movieInstanceService.movieInst2(id,version);
        return new ListDataResponse<>(playRoleBookInstanceService.books(mi.getId()));
    }

    /**
     * 查询角色文件摘要信息
     * @param id 角色id
     * @return 角色文件摘要信息
     */
    @GetMapping("movie/{movieId}/playRoleBook/{id}/digest")
    public SingleDataResponse<String> digest(@RequestHeader Token token, @PathVariable Long movieId, @PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,movieId);
        String digest = playRoleBookInstanceService.digest(movieInstanceId,id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    /**
     * 查询角色文件摘要信息
     * @param id 角色id
     * @return 角色文件摘要信息
     */
    @GetMapping("movie/{movieId}/v/{version}/playRoleBook/{id}/digest")
    public SingleDataResponse<String> digest2(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        String digest = playRoleBookInstanceService.digest(mi.getId(),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(digest);
        return res;
    }

    /**
     * 查询角色文件的存储路径
     * @param id 角色id
     * @return 角色文件存储路径
     */
    @GetMapping("movie/{movieId}/v/{version}/playRoleBook/{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        String storePath = playRoleBookInstanceService.storePath(mi.getId(),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        res.setData(storePath);
        return res;
    }

    /**
     * 下载角色文件
     * @param token 操作用户
     * @param response 响应
     * @param movieId 剧本id
     * @param id 角色id
     */
    @GetMapping("movie/{movieId}/playRoleBook/{id}/file")
    public void downloadFile(@RequestHeader Token token, HttpServletResponse response, @PathVariable Long movieId, @PathVariable Long id){
        Long movieInstanceId = movieDownloadService.loadDownloadInstance(token,movieId);
        playRoleBookInstanceService.downloadFile(response,movieInstanceId,id);
    }

    /**
     * 下载角色文件
     * @param response 响应
     * @param movieId 剧本id
     * @param id 角色id
     */
    @GetMapping("movie/{movieId}/v/{version}/playRoleBook/{id}/file")
    public void downloadFile2(HttpServletResponse response, @PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        playRoleBookInstanceService.downloadFile(response,mi.getId(),id);
    }
}
